home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.lib;
-
- import sub_arctic.input.*;
- import sub_arctic.output.*;
- import sub_arctic.constraints.constraint;
-
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Color;
-
- /**
- * These are toggles which occupy a fixed width and have a
- * label just to the right of the image. They support the style
- * interface and can be used to create either labeled check boxes
- * or labeled radio_buttons.
- *
- * @author Ian Smith
- */
- public class label_toggle extends toggle {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Separation between toggle and label. */
- protected static int _default_separation=3;
-
- /**
- * Return the separation between label and toggle.
- * @return int the number of pixels between label and toggle
- */
- public int default_separation() { return _default_separation;};
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The font for this object. If you don't supply a font, the one chosen
- * for you is the one returned from the style_manager.
- */
- protected Font _font;
-
- /**
- * Return the font for this object. It defaults to being the
- * one set in the style_manager.
- *
- * @return Font the font being used for this object.
- */
- public Font font() {
- if (_font!=null) {
- return _font;
- } else {
- return style_manager.default_font();
- }
- }
-
- /**
- * Set the font in use for this toggle. If you pass null, you'll get
- * the default font from the style_manager.
- *
- * @param Font f the new font
- */
- public void set_font(Font f) {
- _font=f;
- compute_images();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * The text of the label.
- */
- protected String _text;
-
- /**
- * Access the text of the label.
- * @return String the text of the label
- */
- public String text() { return _text;};
-
- /**
- * Modify the string in use by the label
- * @param String s the string to change the label to.
- */
- public void set_text(String s) {
- _text=s;
- compute_images();
- };
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Requested width is the total width of the interactor or -1
- * if we don't care and want to size by string.
- */
- protected int _requested_width;
-
- /**
- * Return the width the creator of the label requested.
- * @return int the size the programmer wanted for this label
- */
- public int requested_width() { return _requested_width;};
-
- /**
- * Set the width to use for labels.
- * @param int w the new width of the label
- */
- public void set_requested_width(int w) {
- _requested_width=w;
- compute_images();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * If this is true, we are going to use the image of
- * radio_button otherwise we use the image of a checkbox.
- */
- protected boolean is_radio_button=true;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * This builds the images of the text & toggle.
- * It computes the height from the font metric.
- */
- protected void compute_images() {
- FontMetrics metric=manager.get_metrics(font());
- int font_height = metric.getHeight() - metric.getLeading();
- int font_ascent = metric.getAscent();
- // we assume that all states of the toggle are the same size
- loaded_image[] result, icon;
- int image_height=font_height,image_width,y;
- drawable d;
-
- /* figure out which set of images we should be using */
- icon = images();
-
- /* how wide should it be? */
- if (requested_width()==-1) {
- /* size by string */
- image_width=metric.stringWidth(text())+icon[0].width()+
- default_separation();
- } else {
- /* user told us to use a value */
- image_width=requested_width();
- }
-
- /* is the font smaller than the appearance of the icon? */
- if (image_height<icon[0].height()) {
- image_height=icon[0].height();
- }
-
- /* build the image */
- result=new loaded_image[2];
- if ((image_width==0) || (image_height==0)) {
- System.out.println("Warning: Detected a zero width and/or height " +
- "label toggle. ");
- }
- result[0]=new loaded_image(image_width, image_height);
- result[1]=new loaded_image(image_width, image_height);
-
- /* draw the icon on #0 */
- d=result[0].get_drawable();
-
- /* center it in y*/
- y=(image_height-icon[0].height())/2;
- d.drawImage(icon[0],0,y);
-
- /* set the font and color */
- d.setColor(Color.black);
- d.setFont(font());
-
- /* put in the separation and draw the text */
- d.drawString(text(),icon[0].width()+default_separation(),
- ((image_height-font_height)/2)+font_ascent);
-
- /* draw the icon on #1 */
- d=result[1].get_drawable();
-
- /* center it in y*/
- y=(image_height-icon[1].height())/2;
- d.drawImage(icon[1],0,y);
-
- /* set the font and color */
- d.setFont(font());
- d.setColor(Color.black);
-
- /* put in the separation and draw the text */
- d.drawString(text(),icon[1].width()+default_separation(),
- ((image_height-font_height)/2)+font_ascent);
-
- /* make the state look in the superclass */
- set_looks(result,null);
- }
-
- /* holds the images for this label toggle */
- protected loaded_image[] _images;
-
- /* returns the right set of images */
- public loaded_image[] images() {
- if (_images == null) {
- /* use the style defaults */
- if (is_radio_button) {
- return (style_manager.current_style().radio_button_make_images());
- }else {
- /* we're a toggle */
- return (style_manager.current_style().checkbox_make_images());
- }
- } else {
- /* use the ones we were given */
- return _images;
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a new label_toggle. Full constructor.
- *
- * @param int x the x position of the object.
- * @param int y the y position of the object.
- * @param boolean radio true if you want the look of a radio
- * button, false if you want the look of a
- * checkbox.
- * @param callback_object call_obj the object to deliver callbacks to.
- * @param int width the width of the label in pixels.
- * @param Font f the font to use for this object (or pass
- * null to get the default font).
- * @param String l the label's string.
- */
- public label_toggle(int x, int y, boolean radio,
- callback_object call_obj, int width,
- Font f, String l)
- {
-
- /* do the super initialization */
- super(x,y,call_obj);
-
- _requested_width=width;
-
- /* set which kind we are */
- is_radio_button=radio;
-
- /* set the font */
- _font=f;
-
- /* now fix the text and draw the images */
- set_text(l);
- }
-
- //had:
- //* @exception bad_value PROPAGATED
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a new label_toggle. You don't need to supply the coordinates
- * because we assume you are using constraints to locate the object
- * on the screen.
- *
- * @param callback_object call_obj the object to deliver callbacks to.
- * @param int width the width of the label in pixels.
- * @param Font f the font to use for this object (or pass
- * null to get the default font).
- * @param String l the label's string.
- */
- public label_toggle(boolean radio,callback_object call_obj, int width,
- Font f, String l)
- {
-
- /* do the super initialization */
- super(0,0,call_obj);
-
- is_radio_button=radio;
- _requested_width=width;
-
- /* set the font */
- _font=f;
-
- /* now fix the text and draw the images */
- set_text(l);
- }
-
- //had:
- //* @exception bad_value PROPAGATED
- //* @exception general PROPAGATED
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Same as full constructor, but takes a pair of images to use for the
- * display of the interactor.
- *
- * @param int x the x position of the object.
- * @param int y the y position of the object.
- * @param boolean radio true if you want the look of a radio
- * button, false if you want the look of a
- * checkbox.
- * @param callback_object call_obj the object to deliver callbacks to.
- * @param int width the width of the label in pixels.
- * @param Font f the font to use for this object (or pass
- * null to get the default font).
- * @param String l the label's string.
- * @param loaded_image[] im the array of images to use for this interactor's
- * display (array must be of size 2)
- */
- public label_toggle(int x, int y, boolean radio,
- callback_object call_obj, int width,
- Font f, String l, loaded_image[] im)
- {
- /* do the super initialization */
- super(x,y,call_obj);
- _requested_width=width;
- /* set which kind we are */
- is_radio_button=radio;
- /* set the font */
- _font=f;
- /* set the images */
- _images = im;
- /* now fix the text and draw the images */
- set_text(l);
- }
-
- /**
- * Constructor which makes a label toggle from a pair of images and
- * doesn't require x and y coordinates.
- *
- * @param callback_object call_obj the object to deliver callbacks to.
- * @param int width the width of the label in pixels.
- * @param Font f the font to use for this object (or pass
- * null to get the default font).
- * @param String l the label's string.
- * @param loaded_image[] im the array of images to use for this interactor's
- * display (array must be of size 2)
- */
- public label_toggle(boolean radio,callback_object call_obj, int width,
- Font f, String l, loaded_image[] im)
- {
- /* do the super initialization */
- super(0,0,call_obj);
- is_radio_button=radio;
- _requested_width=width;
- /* set the font */
- _font=f;
- /* set the images */
- _images = im;
- /* now fix the text and draw the images */
- set_text(l);
- }
-
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-